package jdmx.framework.service;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import jdmx.framework.common.Envelope;
import jdmx.framework.common.Handler;

public abstract class Bo extends Handler {
	protected Dao dao;
	
	@Override
	public void init(String handlerCode)
	{
		super.init(handlerCode);
		if (dao == null) dao = getDao();
	}
	
	protected Dao getDao() {
		return null;
	}
	
	public Envelope execute(Envelope envelope)
	{
		String methodName = envelope.methodName;
		Envelope resultEnvelope = null;
		
		Method method = null;
		try {
			method = (Method) this.getClass().getMethod(methodName, Envelope.class);
		} catch (IllegalArgumentException e1) {
		} catch (SecurityException e1) {
		} catch (NoSuchMethodException e) {
		}		
		
		if (method != null) {
			try {
				resultEnvelope = (Envelope) method.invoke(this, envelope);
			} catch (IllegalArgumentException e) {
			} catch (IllegalAccessException e) {
			} catch (InvocationTargetException e) {
			}
			return resultEnvelope;
		}
		
		return dao.execute(envelope);
	}
}
